package 晚间作业0316.Doc12_课后习题;

import 晚间作业0316.Doc11_实战案例一.Doc11;

import java.util.AbstractMap;
import java.util.AbstractSet;


public class Doc12_04 {

    /**
     * 4. 已知字符串："this is a test of java". 按要求执行以下操作：
     * (1) 【简】统计该字符串中字母s出现的次数
     * (2) 【简】取出子字符串"test"
     * (3) 【简】将本字符串复制到一个字符数组Char[] str中.
     * (4) 【中】将字符串中每个单词的第一个字母变成大写， 输出到控制台。
     */
    public static void main(String[] args) {
        String str = "this is a test of java";
        // (1)
        System.out.println(countSpecifiedChar(str, 's'));
        // (2)
        int begin = str.indexOf("test");
        System.out.println(str.substring(begin, begin + 4));
        // (3)
        char[] chars = str.toCharArray();
        System.out.println(chars);
        // (4)
        String[] split = str.split("\\s+");
        StringBuilder builder = new StringBuilder();
        for (String s : split) {
            builder.append(s.substring(0, 1).toUpperCase());
            if (s.length() > 1) {
                builder.append(s.substring(1));
            }
            builder.append(" ");
        }
        String result = builder.toString();
        if (result.endsWith(" ")) {
            result = result.substring(0, result.length() - 1);
        }
        System.out.println(result);
    }

    public static int countSpecifiedChar(String str, int s) {
        char[] chars = str.toCharArray();
        int count = 0;
        for (char item : chars) {
            if (item == s) {
                count++;
            }
        }
        return count;
    }
}
